﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace Byond.UIRuntime
{
	static class ControlPropertyManager
	{
		private readonly static List<PropertyRegistration> _properties = new List<PropertyRegistration>();

		public static PropertyRegistration<TControl> RegisterProperty<TControl>(string propertyName)
		{
			var propertyInfo = typeof(TControl).GetProperty(propertyName);
			if (propertyInfo == null)
				throw new Exception("Property expected: " + propertyName);

			var propertyRegistration = new PropertyRegistration<TControl>(propertyInfo);
			_properties.Add(propertyRegistration);

			return propertyRegistration;
		}

		public static PropertyRegistration TryGetProperty(PropertyInfo property)
		{
			return _properties
				.FirstOrDefault(it => it.PropertyInfo.MetadataToken == property.MetadataToken);
		}
	}
}
